<template>
	<view class="container">
		<!-- 头部页签 -->
		<view class="header">
			<my-tab :tabData="tabData" :tabCurrent="tabCurrent" @handleClick="handleTabClick"></my-tab>
		</view>
		<!-- 颈部查询 -->
		<view class="navbar">
			<u-form label-width='100'>
				<u-form-item label="类型：">
					<u-input v-model="form.ReceiptTypelabel" type="select" placeholder="请选择" border
						@click="selectConfig.show=true"></u-input>
					<u-select v-model="selectConfig.show" :list="selectConfig.items"
						@confirm="confirm($event)"></u-select>
					<u-button type="primary" class="search_input" size="mini" @click.native="getApsData">查询</u-button>
					<u-button type="success" size="mini" @click.native="openInbound()"
						v-if="hasPermission">新建入库单</u-button>
				</u-form-item>
			</u-form>
		</view>
		<!-- 主视图区表格 -->
		<view class="content" :style="{ height: contentHeight}">
			<view class="table-head">
				<u-table style="z-index: 99;">
					<u-tr>
						<u-th v-for="(col,colIndex) in tableDatas.columns" :key="colIndex" :width="col.width">
							{{col.label}}
						</u-th>
					</u-tr>
				</u-table>
			</view>
			<view class="table-body">
				<scroll-view scroll-y="true">
					<u-table font-size="24">
						<u-tr v-for="(row,rowIndex) in tableDatas.datas" :key="rowIndex">
							<u-td v-for="(col,colIndex) in tableDatas.columns" :key="colIndex" :width="col.width"
								:class="'col-'+colIndex" @click.native="handleRowClick($event,row,col)">
								{{ col.prop==='ReceiptDate'? formatDate(row[col.prop]):row[col.prop]||'-'}}
							</u-td>
						</u-tr>
					</u-table>
				</scroll-view>
			</view>
		</view>
	</view>
</template>

<script>
	import myTab from '@/components/myTabs/index.vue'
	export default {
		components: {
			myTab,
			// inbound
		},
		data() {
			return {
				tabData: ['草稿', '已生效'],
				tabCurrent: 0,
				form: {
					ReceiptType: ''
				},
				selectConfig: {
					show: false,
					items: [{
							label: '全部',
							value: ''
						},
						{
							label: '采购入库',
							value: '采购入库'
						},
						{
							label: '生产入库',
							value: '生产入库'
						},
						{
							label: '销售退货',
							value: '销售退货'
						},
						{
							label: '还回入库',
							value: '还回入库'
						},
						{
							label: '期初入库',
							value: '期初入库'
						},
						{
							label: '调拨入库',
							value: '调拨入库'
						},
						{
							label: '盘盈',
							value: '盘盈'
						},

					],
				},
				contentHeight: '50vh', // 
				tableDatas: {
					dicID: 3006,
					datas: [],
					columns: [{
							prop: 'ReceiptDate',
							label: '日期',
							width: '20%'
						},
						{
							prop: 'ReceiptType',
							label: '类型',
							width: '17%'
						},
						{
							prop: 'ReceiptID',
							label: '单据编号',
							width: '28%'
						},
						{
							prop: 'ReceiptQty',
							label: '数量',
							width: '18%'
						},
						{
							prop: 'CreatedByName',
							label: '创建人',
							width: '17%'
						},

					],
					formData: {},
					loading: false,
					lastClickTime: 0, // 上次点击时间戳
					clickTimer: null, // 单击定时器
					pendingRow: null, // 待处理的行数据
				},
				maskConfig: {
					show: false,
					isEdit: false, // 点击行进入属于编辑，新增不属于
					needEdit: true, // 草稿需要编辑 ，已提交不需要编辑
					rowDatas: null, // 点击行
				},
				hasPermission: false,
			}
		},
		onLoad() {
			const resulut = this.$store.getters.userInfo.RoleMap?.some(val => val.RoleID === 'E01Admin' || val.RoleID ===
				'R2309260003') // 管理员和仓管人员
			this.$set(this, 'hasPermission', resulut)
			this.getApsData()
		},
		onShow() {
			this.getApsData()
		},
		mounted() {
			this.$nextTick(() => {
				this.calculateContentHeight()
			})
		},
		methods: {
			formatDate(val) {
				return this.$u.timeFormat(new Date(val), 'mm-dd')
			},
			//点击页签
			handleTabClick(index) {
				this.tabCurrent = index
				this.getApsData()
			},
			// 获取数据
			async getApsData(remark = 0) {
				const condition = {
					dicID: this.tableDatas.dicID,
					OperationStatus: this.tabCurrent === 0 ? 0 : 2
				}
				Object.assign(condition, this.form)
				this.tableDatas.loading = true
				const res = await this.$request.post({
					url: '/APSAPI/APSData',
					data: condition
				}).catch(err => err)
				this.tableDatas.loading = false
				if (!res) return
				const {
					result,
					msg,
					data
				} = res
				if (!result) return uni.showToast({
					title: msg,
					icon: 'error'
				})
				data.forEach(val => {
					// val.ReceiptDate = this.$u.timeFormat(new Date(val.ReceiptDate), 'yyyy-mm')
				})
				this.$set(this.tableDatas, 'datas', data)
				// this.$set(this.tableDatas, 'columns', Columns[0])
			},
			// 动态计算content height
			calculateContentHeight() {
				// 获取 main-container 的高度
				const query = uni.createSelectorQuery().in(this);
				query.select('.container').boundingClientRect();
				query.select('.header').boundingClientRect();
				query.select('.navbar').boundingClientRect();
				query.exec((rects) => {
					const containerHeight = rects[0]?.height || 0;
					const headerHeight = rects[1]?.height || 0;
					const navbarHeight = rects[2]?.height || 0;
					// 计算剩余高度 = 容器高度 - 头部高度 - 颈部高度 - 其他可能需要减去的间距
					this.contentHeight = containerHeight - headerHeight - navbarHeight + 'px'
				});

			},
			// 行点击事件处理
			handleRowClick(e, row, col) {
				if (col.prop !== 'ReceiptID') return
				this.maskConfig.show = true
				this.maskConfig.isEdit = true
				this.maskConfig.needEdit = row.OperationStatus === 0
				this.maskConfig.rowDatas = row
				this.$u.route({
					url: '/pages/Isgo/inbound/index',
					params: {
						maskConfig: JSON.stringify(this.maskConfig) // 转成字符串传递
					}
				});

			},
			// 打开新建入库单
			openInbound() {
				this.maskConfig.show = true
				this.maskConfig.isEdit = false
				this.maskConfig.needEdit = true
				this.maskConfig.rowDatas = {}
				this.$u.route({
					url: '/pages/Isgo/inbound/index',
					params: {
						maskConfig: JSON.stringify(this.maskConfig) // 转成字符串传递
					}
				});
			},
			//关闭弹窗
			closeMask() {
				this.maskConfig.show = false
				this.getApsData()
			},
			// 选择类型
			confirm(e) {
				let selectData = e[0].value
				// 显示label
				this.$set(this.form, 'ReceiptTypelabel', e[0].label)
				this.$set(this.form, 'ReceiptType', selectData)
			}

		}
	}
</script>

<style lang="scss" scoped>
	* {
		box-sizing: border-box;
	}

	.container {
		display: flex;
		flex-direction: column;
		height: 100%;
		background-color: $uni-bg-color;
		position: relative;
		overflow: hidden;

		.header {}

		.navbar {
			margin-top: 80rpx;
			padding: 0 20rpx;

			::v-deep .u-input__input {
				min-height: 0 !important;
			}

			::v-deep .u-form-item {
				line-height: 24rpx !important;
			}

			.u-form {
				display: flex;
				align-items: center;
			}

			border:1px solid transparent;

			.search_input {
				margin: 0 10rpx;
			}
		}

		.content {
			flex: 1;
			display: flex;
			flex-direction: column;
			padding: 0 10rpx 20rpx;
			overflow: hidden;

			.table-head {}

			.table-body {
				flex-grow: 1;
				width: 100%;
				overflow: hidden;

				uni-scroll-view {
					height: 100%;
				}

				.u-table {
					width: 100%;
					overflow-y: auto;
					overflow-x: hidden;
				}
			}

			.u-td,
			.u-th {
				white-space: nowrap !important;
				overflow: hidden !important;
				text-overflow: ellipsis !important;
				min-width: 0 !important;
				/* 关键属性 */
				flex-shrink: 0 !important;
				/* 防止flex容器压缩 */
			}

			.u-td {
				// max-height: 50rpx;
				height: 60rpx;
			}

			.col-2 {
				// font-size: 18rpx !important;
				white-space: pre-wrap !important;
				color: $uni-color-primary !important;
				text-decoration: underline !important;
				
			}

			.col-3 {
				background-color: #ffff7f;
				
			}
			.col-2,.col-1,.col-4,.col-3,.col-0{
				font-weight: bold;
			}
		}

		.myMask {
			position: absolute;
			top: 0;
			display: flex;
			justify-content: center;
			align-items: center;
			width: 100vw;
			height: 100%;
			z-index: 10000;
			// background-color: rgb(0, 0, 0, 0.5);
			background-color: $uni-bg-color;
		}
	}
</style>